package com.yixun.qfbao.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.yixun.qfbao.model.dto.ApiLogDto;
import com.yixun.qfbao.dao.ApiLogDao;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

//@Component
//@Aspect
public class V10LogInterceptor {
    @Autowired
    ApiLogDao apiLogDao;
    @Pointcut("@annotation(com.yixun.qfbao.annotations.V10Log)")
    public void pointcut() { }

    @Around("pointcut() && args(request,..)")
    public Object addApiLog(ProceedingJoinPoint pjp, HttpServletRequest request)throws Throwable{
       Object[]  methodParam = pjp.getArgs();
        ApiLogDto apiLogDto = new ApiLogDto();
        apiLogDto.setUrl(request.getRequestURI());
        apiLogDto.setCreateTime(new Date());
        apiLogDto.setContent(JSONObject.toJSONString(methodParam[1]));
        apiLogDto.setRemark("渠道请求api");
        apiLogDao.create(apiLogDto);
        Object o = pjp.proceed();
        apiLogDto.setRequestId(apiLogDto.getId());
        apiLogDto.setId(null);
        apiLogDto.setRemark("渠道返回api");
        apiLogDto.setContent(o.toString());
        apiLogDto.setCreateTime(new Date());
        apiLogDao.create(apiLogDto);
        return o;
    }
}
